﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using Microsoft.Win32;


namespace CustomAction
{
    [RunInstaller(true)]
    public partial class CustomAction : System.Configuration.Install.Installer
    {
        RegistryKey manifestRegistryKey;
        string AddInName = "SharePointNetworkTopology";
        string VSTOName = "SPNetworkTopology";

        public CustomAction()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Install
        /// </summary>
        /// <param name="stateSaver">stateSaver</param>
        public override void Install(IDictionary stateSaver)
        {
            // Call the base implementation.
            base.Install(stateSaver);

            string allUsersString = this.Context.Parameters["allUsers"];

            string targetDir = this.Context.Parameters["targetDir"];


            if (String.IsNullOrEmpty(targetDir))
                throw new InstallException("Cannot set the security policy. The specified target directory is not valid.");

            if (stateSaver == null)
                throw new ArgumentNullException("stateSaver");

            try
            {
                bool allUsers = String.Equals(allUsersString, "1");

                try
                {

                    manifestRegistryKey = Registry.CurrentUser.OpenSubKey(String.Format("Software\\Microsoft\\Visio\\Addins\\{0}", this.AddInName), true);
                    if (manifestRegistryKey != null)
                        manifestRegistryKey.SetValue("Manifest",
                            string.Concat(targetDir, String.Format("{0}.vsto|vstolocal", this.VSTOName)),
                            RegistryValueKind.String);
                }
                catch { }
                stateSaver.Add("allUsers", allUsers);

            }
            catch (Exception ex)
            {
                throw new InstallException("Cannot set the security policy.", ex);
            }
        }

        public override void Rollback(System.Collections.IDictionary savedState)
        {
            // Call the base implementation.
            base.Rollback(savedState);

            // Check whether the "allUsers" property is saved.
            // If it is not set, the Install method did not set the security policy.
            if ((savedState == null) || (savedState["allUsers"] == null))
                return;

            try
            {
                bool allUsers = (bool)savedState["allUsers"];
                manifestRegistryKey = Registry.CurrentUser.OpenSubKey(String.Format("Software\\Microsoft\\Visio\\Addins\\{0}", this.AddInName), true);
                if (manifestRegistryKey != null)
                    manifestRegistryKey.DeleteValue("Manifest", false);

            }
            catch (Exception ex)
            {
                throw new InstallException("Cannot remove the security policy.", ex);
            }
        }


        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            // Call the base implementation.
            base.Uninstall(savedState);

            // Check whether the "allUsers" property is saved.
            // If it is not set, the Install method did not set the security policy.
            if ((savedState == null) || (savedState["allUsers"] == null))
                return;

            try
            {
                bool allUsers = (bool)savedState["allUsers"];
                manifestRegistryKey = Registry.CurrentUser.OpenSubKey(String.Format("Software\\Microsoft\\Visio\\Addins\\{0}", this.AddInName), true);
                if (manifestRegistryKey != null)
                    manifestRegistryKey.DeleteValue("Manifest", false);
            }
            catch (Exception ex)
            {
                // Note that throwing an exception might stop the uninstall process.
                // To inform the user and stop the uninstall process, throw an exception.
                // To continue the uninstall, do not throw the exception.
                throw new InstallException("Cannot remove the security policy.", ex);
            }
        }
    }
}
